<?php

//global $_configure_mode;

// Extract Criteria Options
$configparams = array();

$configparams["SW_PROJECT_PASSWORD"] = $_criteria["projectpassword"]->get_criteria_value("VALUE", false);
$configparams["SW_DB_DRIVER"] = $_criteria["dbtype"]->get_criteria_value("VALUE", false);
$configparams["SW_DB_DATABASE"] = $_criteria["database"]->get_criteria_value("VALUE", false);
$configparams["SW_DB_HOST"] = $_criteria["host"]->get_criteria_value("VALUE", false);
$configparams["SW_DB_SERVER"] = $_criteria["server"]->get_criteria_value("VALUE", false);
$configparams["SW_DB_USER"] = $_criteria["user"]->get_criteria_value("VALUE", false);
$configparams["SW_DB_PASSWORD"] = $_criteria["password"]->get_criteria_value("VALUE", false);
$configparams["SW_DB_PROTOCOL"] = $_criteria["protocol"]->get_criteria_value("VALUE", false);
$configparams["SW_HTTP_BASEDIR"] = $_criteria["baseurl"]->get_criteria_value("VALUE", false);
$configparams["SW_PROJECT"] = $_criteria["project"]->get_criteria_value("VALUE", false);
$configparams["SW_PROJECT_TITLE"] = $_criteria["projtitle"]->get_criteria_value("VALUE", false);
$configparams["SW_STYLESHEET"] = $_criteria["stylesheet"]->get_criteria_value("VALUE", false);
$configparams["SW_DB_DATEFORMAT"] = $_criteria["dbdateformat"]->get_criteria_value("VALUE", false);
$configparams["SW_PREP_DATEFORMAT"] = $_criteria["displaydateformat"]->get_criteria_value("VALUE", false);


if ( !$configparams["SW_PROJECT"] ) { trigger_error ( "Specify Project Name" ); return; }
if ( !$configparams["SW_PROJECT_TITLE"] ) { trigger_error ( "Specify Project Title" ); return; }
if ( !$configparams["SW_DB_DRIVER"] ) { trigger_error ( "Specify Database Type" ); return; }
if ( !$configparams["SW_DB_DATABASE"] ) { trigger_error ( "Specify Database Name" ); return; }
if ( !$configparams["SW_DB_HOST"] ) { trigger_error ( "Specify Database Host" ); return; }
if ( !$configparams["SW_DB_USER"]  && $configparams["SW_DB_DRIVER"] != "pdo_sqlite3" ) { trigger_error ( "Specify Database User" ); return; }
if ( !$configparams["SW_HTTP_BASEDIR"] ) { trigger_error ( "Specify Base URL" ); return; }
global $g_debug_mode;
global $g_no_sql;
$g_debug_mode = true;
$g_no_sql = true;

$test = new reportico_datasource("array", "localhost");

$test->driver = $configparams["SW_DB_DRIVER"];
$test->user_name = $configparams["SW_DB_USER"];
$test->password = $configparams["SW_DB_PASSWORD"];
$test->host_name = $configparams["SW_DB_HOST"];
$test->database = $configparams["SW_DB_DATABASE"];
$test->server = $configparams["SW_DB_SERVER"];
$test->protocol = $configparams["SW_DB_PROTOCOL"];
$test->connect(true);

if ( $test->connected )
    handle_debug("Connection to Database succeeded", 0);
else
{
   return;
   trigger_error("Connection to Database failed");
}

$proj_parent = find_best_location_in_include_path( "projects" );
$proj_dir = $proj_parent."/".$configparams["SW_PROJECT"];
$proj_conf = $proj_dir."/config.php";
$proj_menu = $proj_dir."/menu.php";
$proj_lang = $proj_dir."/lang.php";

$proj_template = $proj_parent."/admin/config.template";
$menu_template = $proj_parent."/admin/menu.template";
$lang_template = $proj_parent."/admin/lang.template";


if ( !file_exists ( $proj_parent ) )
{
    trigger_error ("Projects area $proj_parent does not exist - cannot write project");
    return;
}

if ( !is_writeable ( $proj_parent  ) )
{
    trigger_error ("Projects area $proj_parent is not writeable - cannot write project");
    return;
}

if ( file_exists ( $proj_dir ) )
{
    if ( $_configure_mode == "CREATE" || $_configure_mode == "CREATETUTORIALS" )
    {
        trigger_error ("Projects area $proj_dir already exists - cannot write project - use Configure Project instead");
    	return;
    }
}
else
if ( !mkdir ( $proj_dir ) )
{
    trigger_error ("Failed to create project directory $proj_dir");
    return;
}

if ( !is_writeable ( $proj_dir ) )
{
   if ( ! chmod ( $proj_dir, "u+rwx") )
   {
    trigger_error ("Failed to make project directory $proj_dir writeable ");
   }
}

if ( file_exists ( $proj_conf ) && !is_writeable($proj_conf) )
{
    trigger_error ("Projects configuration file $proj_conf exists but is not writeble. Cannot continue");
    //return;
}

if ( $_configure_mode == "CREATE" || $_configure_mode == "CREATETUTORIALS" )
{
	$txt = file_get_contents($proj_template);
}
else
{
	if ( file_exists ( $proj_conf ) )
		$txt = file_get_contents($proj_conf);
	else
		if ( file_exists ( $proj_template ) )
			$txt = file_get_contents($proj_template);
		else
		{
    			trigger_error ("Cannot find source $proj_conf or $proj_template to configure");
    			return;
		}
}

$matches = array();
foreach ( $configparams as $paramkey => $paramval )
{
	if ( $paramkey == "SW_PROJECT" ) 
		continue;
	$txt = preg_replace ( "/(define.*?$paramkey',).*\);/", "$1'$paramval');", $txt);
}

$retval = file_put_contents($proj_conf, $txt);

if ( $_configure_mode == "CREATE"  || $_configure_mode == "CREATETUTORIALS" )
{
	$txt = file_get_contents($menu_template);
	$retval = file_put_contents($proj_menu, $txt);
	$txt = file_get_contents($lang_template);
	$retval = file_put_contents($proj_lang, $txt);
}

if ( !$configparams["SW_PROJECT_PASSWORD"] ) handle_debug ("Warning - Project password not set - any user will be able to run reports in this project", 0);

if ( $_configure_mode == "CREATETUTORIALS" )
	handle_debug("Tutorials Created Successfully", 0);
else if ( $_configure_mode == "CREATE" )
	handle_debug("Project Created Successfully", 0);
else
	handle_debug("Project Configuration Updated Successfully", 0);

$g_debug_mode = false;





?>
